package tvv.levelup.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;
import tvv.levelup.model.Address;
import tvv.levelup.model.Certificate;
import tvv.levelup.model.Employee;
import tvv.levelup.util.HibernateUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * Created by VLAD on 03.07.2014.
 */
public class EmployeeDao
{
    public Address saveAddress(String country)
    {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        Integer addressID = null;
        Address address = null;

        try {
            tx = session.beginTransaction();
            address = new Address(country);
            addressID = (Integer) session.save(address);
            tx.commit();

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return address;
    }

    public Employee saveEmployee(String name, Address address, Set<Certificate> certificates)
    {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        Integer employeeID = null;
        Employee employee = null;

        try {
            tx = session.beginTransaction();
            employee = new Employee(name, address);
            employee.setCertificate(certificates);
            employeeID = (Integer) session.save(employee);
            tx.commit();

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return employee;
    }



    public List<Employee> getAllEmployees(){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
        List<Employee> employees = new LinkedList<Employee>();

        try
        {
            tx = session.beginTransaction();
            employees = session.createQuery("FROM Employee").list();
        }catch (Exception e)
        {
            e.printStackTrace();
        }

        return employees;
    }

}
